/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/

function BVActionTree()
{
	var divTemp = null;
	var oTemp = null;
	var imgTemp = null;
	
	
	this.m_nChild = 0;
	this.m_oContextMenuNode = null;

	// build context menu
	this.m_divMenuContainer = $create("DIV");
	this.m_divMenuContainer.className = "ContextMenuContainer";
	document.body.appendChild(this.m_divMenuContainer);	
	
	this.m_divMenu = $create("DIV");
	this.m_divMenu.className = "ContextMenu";
	this.m_divMenuContainer.appendChild(this.m_divMenu);

	this.m_divMenuShadow = $create("DIV");
	this.m_divMenuShadow.className = "ContextMenuShadow";
	this.m_divMenuContainer.appendChild(this.m_divMenuShadow);

	divTemp = $create("DIV");
	divTemp.id = "divContextMenu_DeleteNode";
	divTemp.attachEvent("onclick",
						$createDelegate(this, this.selectContextMenu));
	imgTemp = $create("IMG");
	imgTemp.src = "images/contextmenu/remove_10_normal.png";
	imgTemp.width = "10";
	imgTemp.height = "10";
	divTemp.appendChild(imgTemp);
	oTemp = $create("SPAN");
	oTemp.innerHTML = "Delete node";
	divTemp.appendChild(oTemp);
	this.m_divMenu.appendChild(divTemp);
	
	

	document.body.attachEvent("onclick",
							  $createDelegate(this, this.hideContextMenu));
// 	document.body.attachEvent("oncontextmenu",
// 							  $createDelegate(this, this.hideContextMenu));
	
}


BVActionTree.prototype.reset = function()
{
	this.m_nChild = 0;
};


BVActionTree.prototype.addNode = function(oNode, sAction)
{
	oNode.topLevel = true;
	oNode.stepChild = this.m_nChild++;
	oNode.action = sAction;
	oNode.attachEvent("oncontextmenu",
					  $createDelegate(this, this.showContextMenu));
	oNode.attachEvent("onclick",
					  $createDelegate(this, this.select));
	oNode.attachEvent("ondblclick",
					  $createDelegate(this, this.edit));
};


BVActionTree.prototype.selectContextMenu = function()
{
	var oEvent = window.event;
	var oSrc = oEvent.srcElement;
	
	if ("IMG" == oSrc.tagName.toUpperCase() ||
		"SPAN" == oSrc.tagName.toUpperCase())
	{
		oSrc = oSrc.parentNode;
	}
	
	switch (oSrc.id)
	{
		case "divContextMenu_DeleteNode":
			// alert(this.m_oContextMenuNode.stepChild);
			window.oAppEvents.removeNode(this.m_oContextMenuNode.stepChild);
			break;
			
	}
};

BVActionTree.prototype.showContextMenu = function()
{
	var oEvent = window.event;
	var spnActionTree = $get("spnActionTree");
	var x = 0;
	var y = 0;


	x = (30 < oEvent.x) ? 30 : oEvent.x;
	y = oEvent.y;
	
	this.m_divMenuContainer.style.left = x + "px";
	this.m_divMenuContainer.style.top = y + "px";
	this.m_divMenuContainer.style.display = "block";


	this.m_oContextMenuNode = oEvent.srcElement;
// 	while ("LI" != this.m_oContextMenuNode.tagName.toUpperCase() &&
// 		   "undefined" != typeof(this.m_oContextMenuNode.stepChild))
	while ("undefined" == typeof(this.m_oContextMenuNode.stepChild)) 
	{
		this.m_oContextMenuNode = this.m_oContextMenuNode.parentNode;
	}
	
	return false;
};


BVActionTree.prototype.hideContextMenu = function()
{
	this.m_divMenuContainer.style.display = "none";
};

	
BVActionTree.prototype.select = function()
{
	var oEvent = window.event;
	var oElement = window.event.srcElement;

	if ("B" == oElement.tagName.toUpperCase())
	{
		oElement = oElement.parentNode;
	}

	if ("spnLabel" == oElement.id ||
		"IMG" == oElement.tagName.toUpperCase())
	{
		if ("IMG" == oElement.tagName.toUpperCase())
		{
			oChild = oElement.parentNode.parentNode.childNodes[1];
		}
		else
		{
			oChild = oElement.parentNode.childNodes[1];
		}
		if (undefined != oChild)
		{
			if ("" == oChild.style.display ||
				"block" == oChild.style.display)
			{
				oChild.style.display = "none";
			}
			else if ("none" == oChild.style.display)
			{
				oChild.style.display = "block";
			}
			window.event.cancelBubble = true;
		}
	}
};


BVActionTree.prototype.edit = function()
{
	var oDialog = {};
	var sName = null;
	var sValue = null;
	var oElement = window.event.srcElement;
	var xmlEvent = null;
	var xmlNode = null;


	
	if ("true" == oElement.topLevel)
	{
		return;
	}

	// don't do anything when the node's label is double clicked
	if ("spnLabel" == oElement.id ||
		"spnLabel" == oElement.parentNode.id)
	{
		return;
	}

	if ("true" == oElement.name)
	{
		oElement = oElement.parentNode;
	}
	
	nChild = oElement.parentNode.parentNode.stepChild;
	
		
	sName = oElement.childNodes[0].innerHTML;
	sValue = oElement.childNodes[1].data;

	oDialog.name = sName;
	oDialog.value = sValue;
	oDialog.modifiedValue = null;

	window.showModalDialog("modaldialog.php",
						   oDialog,
						   "dialogHeight: 105px;" +
						   "dialogWidth: 350px;" + 
						   "center: yes;" +
						   "edge: sunked;" +
						   "scroll: no;" +
						   "status: no;" +
						   "unadorned: no;");
	
	if (null != oDialog.modifiedValue)
	{
		
		oElement.innerHTML = "<b name=\"true\">" + sName + "</b>" + oDialog.modifiedValue;
		oElement.title = oDialog.modifiedValue;
		xmlEvents = window.oAppEvents.get_xmlEvents();
		xmlNode = xmlEvents.documentElement.childNodes.item(nChild);
		xmlNode.setAttribute(sName, oDialog.modifiedValue);
		this.changeTreeNodeLabel(oElement.parentNode.parentNode, sName, oDialog.modifiedValue);
	}
	window.event.cancelBubble = true;
};


BVActionTree.prototype.changeTreeNodeLabel = function(oNode, sAttribute, sNewValue)
{
	switch (oNode.action)
	{
		case "navigate":
			if ("URL" == sAttribute)
			{
				oNode.childNodes[0].childNodes[2].data = "::" + sNewValue;
				oNode.title = "You went to " + sNewValue;
			}
			break;
			
		case "documentselect":
			if ("highlightedText" == sAttribute)
			{
				oNode.childNodes[0].childNodes[2].data = "::" + sNewValue;
				oNode.title = "Text checkpoint for " + sNewValue;
			}
			break;

	}
};

	

